define(
		[ "PDAppDir/properties", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "PDUtilDir/fileupload/multiFileUpload" ],
		function(Properties, Util, Tool, Slidebar, inputSelect, MFU) {
			function updateSupportInit($scope, $compile){
				return new updateSupport($scope, $compile);
			};
			function updateSupport($scope, $compile){
				this.$scope = $scope;
				this.$compile = $compile;
			};
			updateSupport.fn = updateSupport.prototype;
			updateSupport.fn.init = function(data){
				this.entity = data;
				this.bind = {
					isOk: '',
					images: new Array()
				};
			};
			updateSupport.fn.digest = function(){
				 this.$scope.$digest();
			};
			updateSupport.fn.run = function(data) {
				var _this = this;
				_this.init(data);
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer()
							+ "/static/app/hotel/roomStyle/view/update.html",
					$scope: this.$scope,
					$compile: this.$compile,
					afterLoad : function($Panel) {
						_this.initialization();
					}
				});
			};
			updateSupport.fn.initialization = function(){
				this.baseInit();
			};
			
			updateSupport.fn.baseInit = function() {
				var _this = this;
				//酒店
				$.ajax({
		    		url:getServer()+"/flea/hclv/hotel/getSelfHotels.htmls",
		    		success:function(data){
		    			_this.$scope.cache.hotelArray = data;
		    			inputSelect.dropCache();
						inputSelect({
							id : "addRoomStyleHotelName",
							key : {
								id : "id",
								name : "name",
								data : "data",
							},
							data : _this.$scope.cache.hotelArray,
							simpleData : true,
							// 启用文本搜索
							initData:[_this.entity.uuid],
							searchAble : false,
							callback : function(id) {
								_this.entity.uuid = id[0];
								_this.$scope.updateRoomStyleForm.uuid.$setViewValue(_this.entity.uuid);
							}
						});
		    		}
		    	});
				// 床型
				inputSelect.dropCache();
				inputSelect({
					id : "addRoomStyleTypeName",
					key : {
						id : "dictCode",
						name : "dictName",
						data : "data",
					},
					data : _this.$scope.cache.styleArray,
					simpleData : true,
					initData:[_this.entity.type],
					callback : function(id) {
						var id = id[0];
						_this.entity.type = id;
						_this.$scope.updateRoomStyleForm.type.$setViewValue(_this.entity.type);
					}
				});
				
				$.ajax({
					url : getServer() + "/flea/hclv/resRelation/getFilePathByUuid.htmls",
					data:{'uuid': _this.entity.id},
					success : function(data) {
						var rows = data, len = rows.length;
						if(len > 0){
							for(var i = 0; i < len; i++){
								_this.bind.images.push($.extend(rows[i], {
									path: Properties.URL_FILE_SERVER_URL + rows[i].path
								}));
							}
							_this.bind.isOk = true;
							_this.digest();
						}else{
							_this.bind.isOk = '';
						}
						_this.$scope.updateRoomStyleForm.isOk.$setViewValue(_this.bind.isOk);
					}
				});
				
				MFU.init({
	                placeAt:"AddRoomStyleMultiFileUpload",
	                allowPhyDelete:true,
	                allowDelete:true,
	                allowMC:true,
	                onUploadSuccess:function(file){
	                	$.each(_this.bind.images, function(index, row){
	                    	if(row.key == file.name){
	                    		row = $.extend(row, {
	                    			resouceId: file.swordFileId,
	                    			path: file.swordFilePath
	                    		});
	                    	}
	                    });
	                	_this.bind.isOk = true;
	                	_this.$scope.updateRoomStyleForm.isOk.$setViewValue(true);
	                },
	                onDeleteSavedFile:function(file){
                        if(file == null) {
                            return;
                        }
	                	_this.removeImg(file.swordFileId);
	                    _this.digest();
	                },
	                onFilesQueued: function(files){
	                	var webUploader = this.webUploader, queuedFiles = this.queuedFiles;
                        $.each(files, function(index, file) {
                            if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                                $.each(queuedFiles, function(index, item) {
                                    if(file.id == item.file.id){
                                        item.remove();
                                    }
                                });
                                return true;
                            }
	                		webUploader.makeThumb( file, function( error, src ) {
		                        if ( error ) {
		                        	Util.alert('不能预览');
		                            return;
		                        }
		                        _this.bind.images.push({
		                        	key: file.name,
		                        	src: src
		                        });
		                        _this.digest();
		                    }, 500, 300 );
						});
                        if(queuedFiles.length == 0)
                            Util.alert(ImageSuffixNotSupport);

                        webUploader.upload();
	                }
	            });
			};
			updateSupport.fn.removeImg = function(resouceId) {
				var i = 0;
                $.each(this.bind.images, function(index, row){
                	if(row.resouceId == resouceId){
                		i = index;
                		return
                	}
                });
                this.bind.images.splice(i, 1);
                if(this.bind.images.length == 0){
                	this.bind.isOk = '';
                	this.$scope.updateRoomStyleForm.isOk.$setViewValue('');
                }
			};
			updateSupport.fn.save = function() {
				var _this = this, files = new Array();
				$.each(_this.bind.images, function(index, row){
            		files.push({
            			resouceId: row.resouceId,
            			path: row.path
            		});
                });
				$.ajax({
                    url : getServer() + "/flea/hclv/hotel/hotelRoom/saveOrUpdateRoom.htmls",
                    data : {
                        "model": JSON.stringify(_this.entity), 'files': JSON.stringify(files)
                    },
                    success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
                    	_this.$scope.grid.refresh();
                        Util.alert(data.message);
                        _this.slidebars.close();
                    }
				});
			};
			return updateSupportInit;
		});